package com.gs.djys.inquest.inquestlist;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gs.djys.Base.MyAppliCation;
import com.gs.djys.R;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.adapter.LGRecycleViewAdapter;
import com.gs.djys.adapter.LGViewHolder;
import com.gs.djys.check.checklist.CheckListBean;
import com.gs.djys.check.checklistitem.CheckListItemActivity;
import com.gs.djys.inquest.inquestlistitem.InquestListItemActivity;
import com.gs.djys.mvp.MVPBaseActivity;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class InquestActivity extends MVPBaseActivity<InquestContract.View, InquestPresenter> implements InquestContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.search_img)
    ImageView searchImg;
    @Bind(R.id.serce_lay)
    LinearLayout serceLay;
    @Bind(R.id.view)
    View view;
    @Bind(R.id.recycleview)
    RecyclerView recycleview;
    private int page = 1;
    private String param = "";
    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_inquest;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        goBack();
        setTitleText("现场勘验");
        mPresenter.getData(param, page, InquestActivity.this);
    }

    @Override
    public void getDatas(final CheckListBean Datas) {
        if (Datas!=null){
            if (Datas.getData()!=null){
                if (Datas.getData().getRecords()!=null&&Datas.getData().getRecords().size()>0){
                    LinearLayoutManager manager = new LinearLayoutManager(getContext());
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    recycleview.setLayoutManager(manager);
                    LGRecycleViewAdapter<CheckListBean.DataBean.RecordsBean> adapter = new LGRecycleViewAdapter<CheckListBean.DataBean.RecordsBean>(Datas.getData().getRecords()) {
                        @Override
                        public int getLayoutId(int viewType) {
                            return R.layout.item_inquest;
                        }

                        @Override
                        public void convert(LGViewHolder holder, CheckListBean.DataBean.RecordsBean zCDBean, final int position) {
                            holder.setText(R.id.im, zCDBean.getREQ_NO() + " " + zCDBean.getREQ_STATUS());
                            holder.setText(R.id.te, zCDBean.getVEHICLE_NO() + " " + zCDBean.getCARGO_INFO() + " " + zCDBean.getTOTAL_LENGTH() + "*" + zCDBean.getTOTAL_WEIGHT() + "*" + zCDBean.getTOTAL_HEIGHT() + " ");
                            holder.getView(R.id.ky_te).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(InquestActivity.this, InquestListItemActivity.class);
                                    MyAppliCation.spUtils.put("REQ_NO",Datas.getData().getRecords().get(position).getREQ_NO());
                                    startActivityForResult(intent, 1);
                                }
                            });
                        }
                    };
                    recycleview.setAdapter(adapter);
                    adapter.setOnItemClickListener(R.id.item_layout, new LGRecycleViewAdapter.ItemClickListener() {
                        @Override
                        public void onItemClicked(View view, int position) {
                            Intent intent = new Intent(InquestActivity.this, CheckListItemActivity.class);
                            MyAppliCation.spUtils.put("REQ_NO",Datas.getData().getRecords().get(position).getREQ_NO());
                            startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    @OnClick(R.id.search_img)
    public void onViewClicked() {
        param = etSearch.getText().toString();
        mPresenter.getData(param, page, InquestActivity.this);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            if (requestCode == 1) {
                param = etSearch.getText().toString();
                mPresenter.getData(param, page, InquestActivity.this);
            }
        }
    }
}
